//全局配置
window.mokitAppName = "meshare";
ems.config({
	packages: [{
		name: 'mokit',
		location: ems.resovleUri('./mokit/'),
		main: 'app'
	}, {
		name: 'app',
		location: ems.resovleUri('./'),
		main: 'main'
	}],
	paths: {},
	shim: {}
});

/**
 * 最长加载等待时间
 */
ems.options.maxLoadTime = 30000;

/**
 * 应用启动模块
 */
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";

	var app = require('mokit/app');
	var viewExtends = require('./views/view-extends');
	var controllerExtends = require('./controllers/controller-extends');
	var ajax = require('mokit/ajax');
	var utils = require('mokit/utils');
	var $ = require('mokit/jquery');
	var commonUtils = require('./common/utils');
	var $event = require('mokit/event');
	var styleMgr = require('mokit/style');
	var store = require('mokit/store');
	var langMgr = require('mokit/language');
	var user = require('./models/User');

	/**
	 * 加载配置文件
	 */
	var config = app.config = require('mokit/ems-json!./package.json');

	//放在模型里方便调用 add by Li
	user.serviceUri = config.serviceUri;
	user.syproUri = config.serviceUri + "sypro";

	/**
	 * 在本地记录当前版本，以便在将来更新版本时，如果需要根据版本做某些处理使用
	 * 一般在这段代码前处理，然后，记录新的版本号
	 */
	store.local.set('info/version', config.version);

	/**
	 * 为一些对象实例启用事件
	 */
	$event.use(app);
	$event.use(document);

	/**
	 * 语言配置
	 */
	app.language.addLanguage({
		'zh-cn': './languages/zh-cn',
		'en-us': './languages/en-us',
		"zh-tw": './languages/zh-tw'
	}, module);

	/**
	 * 样式配置
	 */
	app.style.addStyle({
		"default": './styles/default.css'
	}, module);

	/**
	 * 路由配置
	 */
	app.route.addRoute([{
		pattern: '/login',
		target: './controllers/login',
		effect: [27, 27]
	}, {
		pattern: '/reg',
		target: './controllers/reg',
		effect: [1, 2]
	}, {
		pattern: '/home',
		target: './controllers/home',
		effect: [0, 0]
	}, {
		pattern: '/search',
		target: './controllers/global-search',
		effect: [0, 0]
	}, {
		pattern: '/contact',
		target: './controllers/contact',
		effect: [0, 0]
	}, {
		pattern: '/contact/group',
		target: './controllers/contact-group',
		effect: [1, 2]
	}, {
		pattern: '/ucenter',
		target: './controllers/ucenter',
		effect: [0, 0]
	}, {
		pattern: '/ucenter/level',
		target: './controllers/ucenter-level',
		effect: [0, 0]		
	}, {
		pattern: '/ucenter/info',
		target: './controllers/ucenter-info',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/info/project',
		target: './controllers/ucenter-info-project',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/info/works',
		target: './controllers/ucenter-info-works',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/money',
		target: './controllers/ucenter-money',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/money/card',
		target: './controllers/ucenter-money-card',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/money/transfer',
		target: './controllers/ucenter-money-transfer',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/project',
		target: './controllers/ucenter-project',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/project/info/{infoId}',
		target: './controllers/ucenter-project-info',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/project/subtask/{id}',
		target: './controllers/ucenter-project-subtask',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/project/subtask/info/{id}',
		target: './controllers/ucenter-project-subtask-info',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/assignMoney/{id}',
		target: './controllers/ucenter-assignMoney',
		effect: [1, 2]		
	}, {		
		pattern: '/ucenter/addPerson/{id}',
		target: './controllers/ucenter-addPerson',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/project/member/{id}',
		target: './controllers/ucenter-project-member',
		effect: [1, 2]		
	},  {
		pattern: '/ucenter/project/member/contact',
		target: './controllers/ucenter-project-member-contact',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/project/summary/{id}',
		target: './controllers/ucenter-project-summary',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/project/data/{id}',
		target: './controllers/ucenter-project-data',
		effect: [1, 2]	
	}, {
		pattern: '/ucenter/project/setting/{id}',
		target: './controllers/ucenter-project-setting',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/community',
		target: './controllers/ucenter-community',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/community/join',
		target: './controllers/ucenter-community-join',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/schedule',
		target: './controllers/ucenter-schedule',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/message',
		target: './controllers/ucenter-message',
		effect: [1, 2]		
	}, {
		pattern: '/ucenter/collect',
		target: './controllers/ucenter-collect',
		effect: [1, 2]
	}, {
		pattern: '/ucenter/collect/list/{type}',
		target: './controllers/ucenter-collect-list',
		effect: [1, 2]
	}, {	
		pattern: '/ucenter/setting',
		target: './controllers/ucenter-setting',
		effect: [1, 2]				
	}, {
		pattern: '/provider',
		target: './controllers/provider',
		effect: [1, 2]
	},{
		pattern: '/provider/{type}',
		target: './controllers/provider',
		effect: [1, 2]		
	},{
		pattern: '/provider/info/{id}',
		target: './controllers/provider-info',
		effect: [1, 2]
	},{
		pattern: '/works/info/{id}',
		target: './controllers/works-info',
		effect: [1, 2]
	},{
		pattern: '/share',
		target: './controllers/share',
		effect: [1, 2]
	},{
		pattern: '/share/{type}',
		target: './controllers/share',
		effect: [1, 2]
	},{
		pattern: '/article/info/{id}',
		target: './controllers/article-info',
		effect: [1, 2]
	},{
		pattern: '/ask/info/{id}',
		target: './controllers/ask-info',
		effect: [1, 2]
	},{
		pattern: '/project/category',
		target: './controllers/project-category',
		effect: [0, 0]
	},{
		pattern: '/project/positionInfo/{prid}/{pid}',
		target: './controllers/project-position-info',
		effect: [1, 2]
	}, {
		pattern: '/project/list/{cat}',
		target: './controllers/project-list',
		effect: [1, 2]
	}, {
		pattern: '/project/info/{infoId}',
		target: './controllers/project-info',
		effect: [1, 2]
	}, {
		pattern: '/project/type',
		target: './controllers/project-type',
		effect: [0, 0]
	}, {
		pattern: '/project/new',
		target: './controllers/project-new',
		effect: [1, 2]
	}, {
		pattern: '/project/done',
		target: './controllers/project-done',
		effect: [1, 2]
	}], module);

	/**
	 * 设置全局的Ajax Loading
	 */
	ajax.loadingOption = {
		pic: module.resovleUri('./images/loading.gif'),
		color: 'rgba(0,0,0,0)'
	};

	/**
	 * 全局配置
	 */
	ajax.setup({
		$timeout: 10000
	});

	/**
	 * 加载资源
	 */
	var loadRes = function(done) {
		//如果不是移动设备
		if (!window.isDevice) {
			app.lockOrientation = function() {
				console.warn('lockOrientation 仅在移动设备有效');
			};
			app.unlockOrientation = function() {
				console.warn('unlockOrientation 仅在移动设备有效');
			};
			return done();
		}
		//加载 cordova.js 
		require('./cordova', function() {
			/**
			 * 设备初始化完成事件
			 */
			document.on('deviceready', function(event) {
				utils.async(function() {
					commonUtils.setStatusBarColor('white');
				});
				//初始化IM服务
				imkit.init('kj7swf8o7zfd2', function(rs) {
					var token = 'cu67/ON6dSpPu2lo8OpaR2FIJaxx2UPF5ZqSOr05g+LPGcioViXvpqDLXwyPmL/7UgC+2lQ8xhf+jBaX1Rw5cg==';
					//连接 IM 服务
					imkit.connect(token, function(rs) {
						console.log('已连接到 IM 服务');
						imkit.setNavBackgroundColor(0, 0, 0);
					}, function(err) {
						alert('连接服务器失败' + err);
					});
				}, function(err) {
					alert('初始化失败' + err);
				});
				//处理返回键
				document.on("backbutton", function(event) {
					var action = null;
					//如果已锁屏
					if (!app.screenLocked) {
						action = commonUtils.backAction.pop();
					}
					if (action) {
						utils.async(function() {
							action();
						}, 20);
					} else {
						if (navigator.app) {
							var lang = langMgr.current();
							commonUtils.confirm("确认退出应用吗？", function(buttonIndex) {
								if (buttonIndex == 1) {
									navigator.app.exitApp();
								}
							});
						}
					}
					return false;
				});
				//处理状态栏
				if (window.StatusBar) {
					window.StatusBar.styleDefault();
					window.StatusBar.show();
				}
				//解锁屏屏幕方向
				app.unlockOrientation = function() {
					if (window.screen && window.screen.unlockOrientation) {
						window.screen.unlockOrientation();
					}
				};
				//锁定屏幕方向
				app.lockOrientation = function(orientation) {
					orientation = orientation || app.orientation || 'portrait';
					if (window.screen && window.screen.lockOrientation) {
						window.screen.lockOrientation(orientation);
					}
				};
				//屏幕旋转事件
				$event(window).on('orientationchange', function(event) {
					event.orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
					app.orientation = event.orientation;
					app.call('orientationchange', event);
				});
				//应用继续事件
				$event(document).on('resume', function(event) {
					app.call('resume', event);
				});
				//应用暂停事件
				$event(document).on('pause', function(event) {
					app.call('pause', event);
				});
				//加载完成
				if (navigator.splashscreen) {
					utils.async(function() {
						navigator.splashscreen.hide();
					}, 1000);
				}

				//完成回调
				return done();
			});
		});
	};


	var indexController = config.index;
	//如果当前已登录
	var userState = user.getState();
	if (userState != null && userState.token != null) {
		indexController = "/home";
		//检查是否启用了屏锁
		/* 暂不启用
		locker.load();
		if (locker.type != locker.types.none.value && locker.secret != null || locker.secret != '') {
		    indexController = "/unlock";
		}
		*/
	}

	/**
	 * 初始化应用程序
	 */
	app.init({
		preInit: loadRes,
		index: config.index, //indexController,
		style: config.style
	});
	window.app = app;


	/**
	 * 如果是桌面环境
	 */
	if (window.require_node) {
		var gui = require_node('nw.gui');
		var win = gui.Window.get();
		require(['mokit/key'], function(key) {
			//切撒网全屏模式
			key('ctrl+enter', function(event) {
				win.toggleFullscreen();
				return false;
			});
		});
	}
});